package com.waqas.rcp.view.categoryview;

import java.util.ArrayList;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class WaqasCategoryView extends ViewPart {
	public WaqasCategoryView() {
	}

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "com.waqas.rcp.view.categoryview.WaqasCategoryView";
	
	private TableViewer viewer;
	private Table table;

	  
	/**
	   * This is a callback that will allow us to create the viewer and initialize
	   * it.
	   */

	  public void createPartControl(Composite parent) {
	    FormToolkit toolkit = new FormToolkit(parent.getDisplay());
	    ScrolledForm form = toolkit.createScrolledForm(parent);
	    form.setText("Eclipse Forms API Example");
	    
	    table = new Table(form.getBody(), SWT.BORDER | SWT.FULL_SELECTION);
	    table.setBounds(10, 10, 469, 502);
	    toolkit.adapt(table);
	    toolkit.paintBordersFor(table);
	    table.setHeaderVisible(true);
	    table.setLinesVisible(true);

	    // Lets make a layout for the first section of the screen
	    GridLayout layout = new GridLayout();
	    layout.numColumns = 1;
	    layout.marginWidth = 2;
	    layout.marginHeight = 2;
	    // Creating the Screen
	    Section section = toolkit.createSection(parent, Section.DESCRIPTION
	        | Section.TITLE_BAR);
	    section.setText("Section 1 for demonstration"); //$NON-NLS-1$
	    section.setDescription("This demonstrates the usage of section");
	    // Composite for storing the data
	    Composite client = toolkit.createComposite(section, SWT.WRAP);
	    layout = new GridLayout();
	    layout.numColumns = 2;
	    layout.marginWidth = 2;
	    layout.marginHeight = 2;
	    client.setLayout(layout);
	    Table t = toolkit.createTable(client, SWT.NULL);
	    GridData gd = new GridData(GridData.FILL_BOTH);
	    gd.heightHint = 20;
	    gd.widthHint = 100;
	    t.setLayoutData(gd);
	    toolkit.paintBordersFor(client);
	    Button b = toolkit.createButton(client, "Do something", SWT.PUSH); //$NON-NLS-1$
	    b.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent e) {
	    		System.out.println("Button Pressed");
	    	}
	    });
	    gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
	    b.setLayoutData(gd);
	    section.setClient(client);
	    viewer = new TableViewer(t);

	    viewer.setContentProvider(new ArrayContentProvider());

	    TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
	    viewerColumn.getColumn().setWidth(100);
	    viewerColumn.setLabelProvider(new ColumnLabelProvider() {
	      @Override
	      public String getText(Object element) {
	        return element.toString();
	      };

	      public Image getImage(Object element) {
	        return PlatformUI.getWorkbench().getSharedImages()
	            .getImage(ISharedImages.IMG_OBJ_ELEMENT);
	      };
	    });
	    viewer.setInput(getViewSite());
	  }

	  
	/**
	   * Passing the focus request to the viewer's control.
	   */

	  public void setFocus() {
	    viewer.getControl().setFocus();
	  }
}